package com.fsm.file.serviceImpl;

import com.fsm.common.tools.Result.Result;
import com.fsm.file.service.FileService;
import jakarta.servlet.http.HttpServletRequest;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileStorageService fileStorageService;//注入实列
    /**
     * 上传文件
     *
     * @param
     * @return
     */
    @Override
    public Result uploadFile(HttpServletRequest request) throws Exception {
        FileInfo fileInfo = fileStorageService.of(((MultipartHttpServletRequest)request).getFile("file")).upload();
        //如果配置了多个存储平台（如阿里云）
        //FileInfo fileInfo = fileStorageService.of(((MultipartHttpServletRequest)request).getFile("file")).setPlatform("aliyun-oss-1").upload();
        return Result.ok(fileInfo.getUrl());
    }
}
